package ssm.dao;
import javax.jms.*;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


public class ActivemqUtils {

    public static final String brokerURL = "tcp://10.0.13.178:61616";

    public static final String topicDestination = "SNAP_IMAGE_INFO_TOPIC";


    public static MyResult getMessage() throws JMSException {
        // 默认的账号和密码为null
        String username = ActiveMQConnection.DEFAULT_USER;
        String password = ActiveMQConnection.DEFAULT_PASSWORD;
        // 初始化连接工厂, DEFAULT_BROKER_URL =failover://tcp://localhost:61616
        ConnectionFactory factory = new ActiveMQConnectionFactory(username, password, brokerURL);
        // 创建连接
        Connection connection = factory.createConnection();
        connection.start();
        // 创建会话
        Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createTopic(topicDestination);
        // MessageConsumer负责接受消息
        MessageConsumer consumer = session.createConsumer(destination);
        TextMessage message = (TextMessage)consumer.receive();
        if (null != message) {
            String messageString = message.getStringProperty("msgId");
            System.out.println("Receive : " + messageString);
            session.commit();
            return MyResult.ok(messageString);
        }else{
            return MyResult.ok(message);
        }
        // 测试状态,这里把关闭会话和连接注释掉了。
        // session.close();
        // connection.close();
    }


    public static void main(String[] args) {

    }

}

